package com.yueyang.sojourn.server.service.sojourn.order;

import com.yueyang.sojourn.server.service.msg.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.LinkedHashMap;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 订单短信服务
 * @author Andy.yang
 * @version v1
 * @summary
 * @Copyright (c) 2019, YueYang Group All Rights Reserved.
 * @since 2020/3/29
 */
@Service
public class SojournOrderMsgService {
    @Autowired
    SmsService smsService;
    @Autowired
    RedisTemplate redisTemplate;



    private String getDealCaptchaRedisKey(String phone,Long orderId) {
        return "SojournOrderMsgService:sendDealCaptcha:" + phone + ":" + orderId;
    }
    /**
     * 校验验证码
     * @param phone
     * @param captcha
     * @return
     */
    public boolean verifiDealCaptcha(String phone,Long orderId, String captcha) {
        String rightCaptcha = (String) redisTemplate.opsForValue().get(getDealCaptchaRedisKey(phone,orderId));
        return !StringUtils.isEmpty(captcha) && captcha.equals(rightCaptcha);
    }

    /**
     * 订单核销时操作员验证
     * @param phone
     */
    public void sendDealCaptcha(String phone,Long orderId){
        String captcha = String.format("%04d",new Random().nextInt(9999));
        LinkedHashMap datas = new LinkedHashMap();
        datas.put("code",captcha);
        redisTemplate.opsForValue().set(getDealCaptchaRedisKey(phone,orderId), captcha, 5, TimeUnit.MINUTES);
        smsService.sendMsg(phone,datas,smsService.getOrderDealCaptchaTemplateCode());
    }

    private String getRefundCaptchaRedisKey(String phone,Long orderId) {
        return "SojournOrderMsgService:sendRefundCaptcha:" + phone + ":" + orderId;
    }
    /**
     * 校验验证码
     * @param phone
     * @param captcha
     * @return
     */
    public boolean verifiRefundCaptcha(String phone,Long orderId, String captcha) {
        String rightCaptcha = (String) redisTemplate.opsForValue().get(getRefundCaptchaRedisKey(phone,orderId));
        return !StringUtils.isEmpty(captcha) && captcha.equals(rightCaptcha);
    }

    /**
     * 订单退款时操作员验证
     * @param phone
     */
    public void sendRefundCaptcha(String phone,Long orderId){
        String captcha = String.format("%04d",new Random().nextInt(9999));
        LinkedHashMap datas = new LinkedHashMap();
        datas.put("code",captcha);
        redisTemplate.opsForValue().set(getRefundCaptchaRedisKey(phone,orderId), captcha, 5, TimeUnit.MINUTES);
        smsService.sendMsg(phone,datas,smsService.getOrderDealCaptchaTemplateCode());
    }

    /**
     * 订单创建时发送短信
     * @param phone
     */
    public void sendMsgCreated(String phone,String orderSn,String merchName,String travelTime){
        LinkedHashMap datas = new LinkedHashMap();
        datas.put("orderSn",orderSn);
        datas.put("merchName",merchName);
        datas.put("travelTime",travelTime);
        smsService.sendMsg(phone,datas,smsService.getOrderCreatedTemplateCode());
    }

    /**
     * 创建15分钟过期时发送短信
     * @param phone
     */
    public void sendMsgWillExpire(String phone,String orderSn,String merchName,String travelTime){
        LinkedHashMap datas = new LinkedHashMap();
        datas.put("orderSn",orderSn);
        datas.put("merchName",merchName);
        datas.put("travelTime",travelTime);
        smsService.sendMsg(phone,datas,smsService.getOrderWillExpireTemplateCode());
    }

    /**
     * 订单过期取消时发送短信
     * @param phone
     */
    public void sendMsgExpired(String phone,String orderSn,String merchName,String travelTime){
        LinkedHashMap datas = new LinkedHashMap();
        datas.put("orderSn",orderSn);
        datas.put("merchName",merchName);
        datas.put("travelTime",travelTime);
        smsService.sendMsg(phone,datas,smsService.getOrderExpiredTemplateCode());
    }

    /**
     * 订单成交核销时发送短信
     * @param phone
     */
    public void sendMsgDealed(String phone,String orderSn,String merchName,String travelTime){
        LinkedHashMap datas = new LinkedHashMap();
        datas.put("orderSn",orderSn);
        datas.put("merchName",merchName);
        datas.put("travelTime",travelTime);
        smsService.sendMsg(phone,datas,smsService.getOrderDealedTemplateCode());
    }

    /**
     * 订单退款后发送短信
     * @param phone
     */
    public void sendMsgRefunded(String phone,String orderSn,String merchName,String travelTime){
        LinkedHashMap datas = new LinkedHashMap();
        datas.put("orderSn",orderSn);
        datas.put("merchName",merchName);
        datas.put("travelTime",travelTime);
        smsService.sendMsg(phone,datas,smsService.getOrderRefundedTemplateCode());
    }
}
